﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bot.Application.Dto;
using Bot.Application.Services;
using Microsoft.AspNetCore.Mvc;

namespace Bot.WebAPI.Controllers
{
    [Route("api/v3/chatbot")]
    [ApiController]
    public class SmartTriggersController : ControllerBase
    {

        private readonly ISmartTriggerAppService _app;
        public SmartTriggersController(ISmartTriggerAppService app)
        {
            this._app = app;
        }

        [HttpGet]
        [Route("smartTriggers/{id:Guid}")]
        public ActionResult<SmartTriggerDto> Get([FromRoute] Guid id)
        {
            SmartTriggerDto smartTrigger = _app.Get(id);
            return Ok(smartTrigger);
        }

        [HttpGet]
        [Route("bots/{botId:Guid}/smartTriggers")]        
        public ActionResult<IEnumerable<SmartTriggerDto>> GetList([FromRoute] Guid botId, [FromQuery] string include)
        {
            IEnumerable<SmartTriggerDto> list = _app.GetList(botId, include);
            return Ok(list);
        }

        [HttpPut]
        [Route("smartTriggers/{id:Guid}")]
        public ActionResult<SmartTriggerDto> Put([FromRoute] Guid id, [FromBody] SmartTriggerUpdateDto dto)
        {
            SmartTriggerDto smartTrigger = _app.Update(id, dto);
            return Ok(smartTrigger);
        }

        [HttpPost]
        [Route("bots/{botId:Guid}/smartTriggers")]
        public ActionResult<SmartTriggerDto> Post([FromRoute] Guid botId, [FromBody] SmartTriggerCreateDto dto)
        {
            SmartTriggerDto smartTrigger = _app.Add(botId, dto);
            return CreatedAtAction(nameof(Get), new { id = smartTrigger.Id }, smartTrigger);
        }

        [HttpDelete]
        [Route("smartTriggers/{id:Guid}")]
        public ActionResult Delete([FromRoute] Guid id)
        {
            _app.Delete(id);
            return NoContent();
        }
    }
}
